<?php

namespace App\Http\Controllers\Api;

use App\Models\Article;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class SearchController extends Controller
{

    /*
     * 搜索接口
     * @param Request $request
     * @return array
     */
    public function search(Request $request)
    {
        $field = ['id','article_title','list_img','imgs','video_url','category_id','region_id','zan_num','read_num','collect_num','like_num','share_num','content','admin_id','types','imgs'];
        $region_id = $request -> input('region_id');
        $keywords  = $request -> input('keywords');
        $pagesize  = $request -> input('pagesize') ?? 5;


        $res = Article::with(['articles_author:id,name,avatar','article_category'])
            ->where(function ($query) use($region_id){

                $query -> where('region_id',0);
                $query -> orWhere('region_id',null);
                if($region_id){
                    $query  -> orWhere('region_id',$region_id);
                }
                /*if($keywords){
                    $query  -> where();
                }*/

            }) -> where('article_title','like','%'.$keywords.'%') -> where('is_show',1)
            -> select($field) -> orderBy('id','desc') -> paginate($pagesize);


        return jsonSuccess($res);
    }


}
